#include <QDebug>
#include "fuzzytoolbox.h"

FuzzyToolbox::FuzzyToolbox(QObject *parent) : QObject(parent)
{

}

FuzzyToolbox::~FuzzyToolbox()
{

}

float FuzzyToolbox::getSMemberShip(float x, float alpha, float beta, float gamma, bool *ok)
{
    if(x<alpha)
    {
        *ok=true;
        return 0;
    }
    else if(x>=gamma)
    {
        *ok=true;
        return 1;
    }
    else
    {
        if(beta<=alpha||beta>=gamma||(2*beta-(alpha+gamma)>0.01))
        {
            *ok=false;
            qDebug()<<"Error:beta should between alpha and gamma and equal (alpha+gamma)/2";
            return -1;
        }
        else
        {
            if(x<=beta)
            {
                return 2*(x-alpha)*(x-alpha)/(gamma-alpha)/(gamma-alpha);
            }
            else
            {
                return 1-2*(x-gamma)*(x-gamma)/(gamma-alpha)/(gamma-alpha);
            }
        }
    }
}

float FuzzyToolbox::getUnbalancedSMemberShip(float x,float alpha,float beta,float gamma,bool *ok)
{
    if(x<alpha)
    {
        *ok=true;
        return 0;
    }
    else if(x>=gamma)
    {
        *ok=true;
        return 1;
    }
    else
    {
        if(beta<=alpha||beta>=gamma)
        {
            *ok=false;
            qDebug()<<"Error:beta should between alpha and gamma！";
            return -1;
        }
        else
        {
            if(x<=beta)
            {
                return 0.5*(x-alpha)*(x-alpha)/(beta-alpha)/(beta-alpha);
            }
            else
            {
                return 1-0.5*(x-gamma)*(x-gamma)/(gamma-beta)/(gamma-beta);
            }
        }
    }
}
